﻿//-----------------------------------------------------------------------
// <copyright file="MockRepositoryFactory.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings

using System;
using System.Collections.Generic;
using GateShift.Common.Repository.Interfaces.Factories;
using GateShift.Common.Repository.Interfaces.Repositories.Base;
using Moq;

#endregion

namespace GateShift.Testing.Services.GameFramework
{
    public class MockRepositoryFactory : IRepositoryFactory
    {
        private static readonly Dictionary<Type, IMock> _mocks;

        /// <summary>
        /// Initializes MockRepositoryFactory with default values
        /// </summary>
        static MockRepositoryFactory()
        {
            // Initialize variables
            _mocks = new Dictionary<Type, IMock>();
        }

        public static void TestInitialize()
        {
            _mocks.Clear();
        }

        public static void TestCleanup()
        {
            foreach (IMock mock in _mocks.Values)
            {
                mock.VerifyAll();
            }

            _mocks.Clear();
        }

        public static IMock<T> GetMock<T>() where T : class, IRepository
        {
            if (!_mocks.ContainsKey(typeof(T)))
            {
                _mocks.Add(typeof(T), new Mock<T>());
            }

            return (IMock<T>)_mocks[typeof(T)];
        }

        public T GetRepository<T>() where T : class, IRepository
        {
            return GetMock<T>().Object;
        }
    }
}